package com.yanqu.road.dao.impl.activity.hsisland;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandActivityData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandMonsterData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class HsIslandActivityDataDaoImpl extends TempDataObjectDao<HsIslandActivityData> {

    public static String getHsIslandActivityDataSql = "select * from `t_c_activity_hs_island_activity_data` where `activityId`=? and `groupId`=?";
    public HsIslandActivityData getHsIslandActivityData(int activityId,int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandActivityDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHsIslandActivityDataSql = "update `t_c_activity_hs_island_activity_data` set battleValue=?,maxBattleValue=?,bossNum=?,bossData=?,lastBattleValueTime=?,updateUnionAcTime=?,sendReward=? where activityId=? and groupId=?";
    @Override
    protected boolean updateData(HsIslandActivityData hsIslandActivityData) {
        hsIslandActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getBattleValue()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getMaxBattleValue()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandActivityData.getBossNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandActivityData.getBossData() == null ? "" : JSON.toJSONString(hsIslandActivityData.getBossData())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getLastBattleValueTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getUpdateUnionAcTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, hsIslandActivityData.isSendReward()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandActivityData.getGroupId()));
        if (getDb().executeNoneQuery(updateHsIslandActivityDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandActivityDataSql = "insert into `t_c_activity_hs_island_activity_data`(activityId,groupId,battleValue,maxBattleValue,bossNum,bossData,lastBattleValueTime,updateUnionAcTime,sendReward) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandActivityData hsIslandActivityData) {
        hsIslandActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandActivityData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getBattleValue()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getMaxBattleValue()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandActivityData.getBossNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandActivityData.getBossData() == null ? "" : JSON.toJSONString(hsIslandActivityData.getBossData())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getLastBattleValueTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandActivityData.getUpdateUnionAcTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, hsIslandActivityData.isSendReward()));
        if (getDb().executeNoneQuery(insertHsIslandActivityDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandActivityData parseData(ResultSet rs) throws SQLException {
        HsIslandActivityData hsIslandActivityData = new HsIslandActivityData();
        hsIslandActivityData.setActivityId(rs.getInt("activityId"));
        hsIslandActivityData.setGroupId(rs.getInt("groupId"));
        hsIslandActivityData.setBattleValue(rs.getLong("battleValue"));
        hsIslandActivityData.setMaxBattleValue(rs.getLong("maxBattleValue"));
        hsIslandActivityData.setBossNum(rs.getInt("bossNum"));
        String bossData = rs.getString("bossData");
        if (!StringUtils.isNullOrEmpty(bossData)) {
            hsIslandActivityData.setBossData(JSON.parseObject(bossData, new TypeReference<HsIslandMonsterData>() {
            }));
        }
        hsIslandActivityData.setLastBattleValueTime(rs.getLong("lastBattleValueTime"));
        hsIslandActivityData.setUpdateUnionAcTime(rs.getLong("updateUnionAcTime"));
        hsIslandActivityData.setSendReward(rs.getBoolean("sendReward"));

        return hsIslandActivityData;
    }
}
